﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData;
using NotAClue.ComponentModel.DataAnnotations;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace NotAClue.Web.DynamicData
{
    public static class RowHighlightingExtensionMethods
    {
        /// <summary>
        /// Highlights the row.
        /// </summary>
        /// <param name="fieldTemplate">The field template.</param>
        public static void HighlightRow(this FieldTemplateUserControl fieldTemplate)
        {
            // get page template
            var pageTamplte = fieldTemplate.Page.GetPageTemplate();

            var value = fieldTemplate.FieldValueString;

            // get the attribute
            var rowHighlighting = fieldTemplate.MetadataAttributes.GetAttribute<RowHighlightingAttribute>();

            // make sure the attribute is not null
            if (rowHighlighting != null)
            {
                if (pageTamplte == PageTemplate.DetailsList || pageTamplte == PageTemplate.EditList)
                {
                    var htmlTableRow = fieldTemplate.GetContainerControl<HtmlTableRow>();

                    if (htmlTableRow != null && String.Compare(rowHighlighting.ValueWhenTrue, value, true) == 0)
                    {
                        // apply the CSS class appending if a class is already applied.
                        if (!String.IsNullOrWhiteSpace(htmlTableRow.Attributes["class"]))
                            htmlTableRow.Attributes["class"] += " " + rowHighlighting.CssClass;
                        else
                            htmlTableRow.Attributes.Add("class", rowHighlighting.CssClass);
                    }
                }
                else if (pageTamplte == PageTemplate.List)
                {
                    var parentRow = fieldTemplate.GetContainerControl<GridViewRow>();

                    if (parentRow != null && String.Compare(rowHighlighting.ValueWhenTrue, value, true) == 0)
                    {
                        // apply the CSS class appending if a class is already applied.
                        if (String.IsNullOrWhiteSpace(parentRow.CssClass))
                            parentRow.CssClass += " " + rowHighlighting.CssClass;
                        else
                            parentRow.CssClass = rowHighlighting.CssClass;
                    }
                }
            }
        }

        /// <summary>
        /// Highlight the Row based on date of another column.
        /// </summary>
        /// <param name="fieldTemplate">The field template.</param>
        public static void RowHighlightingByDate(this FieldTemplateUserControl fieldTemplate, object row)
        {
            // get the attribute
            var rowHighlighting = fieldTemplate.MetadataAttributes.GetAttribute<RowHighlightingByDateAttribute>();
            // make sure the attribute is not null
            if (rowHighlighting != null)
            {
                try
                {
                    var pageTamplte = fieldTemplate.Page.GetPageTemplate();
                    var currentValue = DateTime.Now;
                    var dataRow = row as ICustomTypeDescriptor;
                    var value = dataRow.GetPropertyOwner(null).GetPropValue(rowHighlighting.CompareToColumn);
                    if (value != null)
                    {
                        var compareColumnValue = (DateTime)value;

                        var isComplete = fieldTemplate.FieldValue != null;
                        if (isComplete)
                            currentValue = (DateTime)fieldTemplate.FieldValue;

                        var result = compareColumnValue - currentValue;
                        if (pageTamplte == PageTemplate.List)
                            SetGridViewRow(fieldTemplate, rowHighlighting, result, isComplete);
                        else if (pageTamplte == PageTemplate.DetailsList)
                            SetHtmlTableRow(fieldTemplate, rowHighlighting, result, isComplete);
                    }
                }
                catch (Exception ex)
                {
                    //TODO:
                    var err = ex;
                }
            }
        }

        /// <summary>
        /// Sets the grid view row.
        /// </summary>
        /// <param name="fieldTemplate">The field template.</param>
        /// <param name="rowHighlighting">The row highlighting.</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        private static void SetGridViewRow(FieldTemplateUserControl fieldTemplate, RowHighlightingByDateAttribute rowHighlighting, TimeSpan result, Boolean isComplete)
        {
            var gridViewRow = fieldTemplate.GetContainerControl<GridViewRow>();
            if (gridViewRow != null)
            {
                if (!isComplete)
                {
                    // set highlighting
                    if (result.Days < 0)
                    {// after compareColumnValue
                        SetGridViewRowClass(gridViewRow, rowHighlighting.PassedDateCss);
                        gridViewRow.ToolTip = String.Format(rowHighlighting.PassedDateText, Math.Abs(result.Days));
                    }
                    else if (result.Days > rowHighlighting.Days)
                    {// before compareColumnValue
                        SetGridViewRowClass(gridViewRow, rowHighlighting.BeforeDateCss);
                        gridViewRow.ToolTip = String.Format(rowHighlighting.BeforeDateText, result.Days);
                    }
                    else if (result.Days < rowHighlighting.Days)
                    {// between days and compareColumnValue
                        SetGridViewRowClass(gridViewRow, rowHighlighting.ApproachingDateCss);
                        gridViewRow.ToolTip = String.Format(rowHighlighting.ApproachingDateText, result.Days);
                    }
                }
                else
                {
                    //var beforeDays = -rowHighlighting.Days;
                    //if (result.Days > beforeDays)
                    if (result.Days >= 0)
                    {
                        SetGridViewRowClass(gridViewRow, rowHighlighting.BeforeDateCss);
                        gridViewRow.ToolTip = String.Format(rowHighlighting.CompleteCss, result.Days);
                    }
                    else
                    {
                        SetGridViewRowClass(gridViewRow, rowHighlighting.AfterDateCss);
                        gridViewRow.ToolTip = String.Format(rowHighlighting.PassedDateCss, result.Days);
                    }
                }
            }
        }


        /// <summary>
        /// Sets the grid view row class.
        /// </summary>
        /// <param name="gridViewRow">The grid view row.</param>
        /// <param name="cssClass">The CSS class.</param>
        internal static void SetGridViewRowClass(GridViewRow gridViewRow, String cssClass)
        {
            if (!String.IsNullOrWhiteSpace(gridViewRow.CssClass))
                gridViewRow.CssClass += " " + cssClass;
            else
                gridViewRow.CssClass = cssClass;
        }

        /// <summary>
        /// Sets the HTML table row.
        /// </summary>
        /// <param name="fieldTemplate">The field template.</param>
        /// <param name="rowHighlighting">The row highlighting.</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        private static void SetHtmlTableRow(FieldTemplateUserControl fieldTemplate, RowHighlightingByDateAttribute rowHighlighting, TimeSpan result, Boolean isComplete)
        {
            var htmlTableRow = fieldTemplate.GetContainerControl<HtmlTableRow>();
            if (htmlTableRow != null)
            {
                if (!isComplete)
                {
                    // set highlighting
                    if (result.Days < 0)
                    {// after compareColumnValue
                        SetHtmlTableRowClass(htmlTableRow, rowHighlighting.PassedDateCss);
                        htmlTableRow.Attributes.Add("title", String.Format(rowHighlighting.PassedDateText, Math.Abs(result.Days)));
                    }
                    else if (result.Days > rowHighlighting.Days)
                    {// before compareColumnValue
                        SetHtmlTableRowClass(htmlTableRow, rowHighlighting.BeforeDateCss);
                        htmlTableRow.Attributes.Add("title", String.Format(rowHighlighting.BeforeDateText, result.Days));
                    }
                    else if (result.Days < rowHighlighting.Days)
                    {// between days and compareColumnValue
                        SetHtmlTableRowClass(htmlTableRow, rowHighlighting.ApproachingDateCss);
                        htmlTableRow.Attributes.Add("title", String.Format(rowHighlighting.ApproachingDateText, result.Days));
                    }
                }
                else
                {
                    //var beforeDays = -rowHighlighting.Days;
                    //if (result.Days > beforeDays)
                    if (result.Days >= 0)
                    {
                        SetHtmlTableRowClass(htmlTableRow, rowHighlighting.CompleteCss);
                        htmlTableRow.Attributes.Add("title", String.Format(rowHighlighting.BeforeDateText, Math.Abs(result.Days)));
                    }
                    else
                    {
                        SetHtmlTableRowClass(htmlTableRow, rowHighlighting.AfterDateCss);
                        htmlTableRow.Attributes.Add("title", String.Format(rowHighlighting.PassedDateText, Math.Abs(result.Days)));
                    }
                }
            }
        }

        /// <summary>
        /// Sets the HTML table row class.
        /// </summary>
        /// <param name="tableRow">The table row.</param>
        /// <param name="cssClass">The CSS class.</param>
        internal static void SetHtmlTableRowClass(HtmlTableRow tableRow, String cssClass)
        {
            if (!String.IsNullOrWhiteSpace(tableRow.Attributes["class"]))
                tableRow.Attributes["class"] += " " + cssClass;
            else
                tableRow.Attributes.Add("class", cssClass);
        }
    }
}
